package main

import (
	"fmt"
)

// 单环链表
type node struct {
	data interface{}
	next *node
}

var head *node //开始节点
var tail *node //结束节点

func main() {
	for i := 0; i < 10; i++ {
		n := &node{i, nil}
		AddNode(n)

	}

	ShowList(head)

	fmt.Println("")
	jose(0, 3)

}

func AddNode(n *node) {
	if tail == nil {
		head = n
		n.next = head
		tail = n
	} else {
		tail.next = n
		n.next = head
		tail = n
	}
}

func ShowList(head *node) {
	if head == nil {
		return
	} else {
		for head.next != nil && head != tail {
			fmt.Println(head.data)
			head = head.next
		}
		fmt.Println(head.data)

	}
}

//从第K个，循环起第num个，留下最后一个
func jose(k, num int) {
	count := 1

	//先到第k  数据
	for i := 0; i < k; i++ {
		head = head.next
		tail = tail.next
	}

	for {
		count++
		head = head.next
		tail = tail.next

		if count == num {
			fmt.Println(head.data, "出局")
			tail.next = head.next
			head = head.next
			count = 1
		}

		if head == tail {
			fmt.Println(head.data, "胜出")
			break
		}

	}
}
